package com.chinaisafe.user.controller;

import cn.hutool.core.lang.Assert;
import com.chinaisafe.base.common.validated.UpdateGroup;
import com.chinaisafe.gateway.sdk.annotation.InnerApi;
import com.chinaisafe.module.user.request.RoleUserReqVO;
import com.chinaisafe.module.user.request.UserRoleReqVO;
import com.chinaisafe.user.biz.SysUserRoleBiz;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.chinaisafe.base.common.annotation.OperationLog;
import com.chinaisafe.base.common.enums.BusinessTypeEnum;
import com.chinaisafe.base.common.vo.Answer;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.chinaisafe.user.service.SysUserRoleService;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * SysUserRoleController
 * Description：用户角色接口层
 *
 * @author xuefu.shu
 * @date  2024-07-13 16:53:56
 */
@RestController
@RequestMapping("/sysUserRole")
@Tag(name = "用户角色接口层", description = "用户角色接口层")
public class SysUserRoleController {


    @Resource
    private SysUserRoleService sysUserRoleService;

    @Resource
    private SysUserRoleBiz sysUserRoleBiz;


    @GetMapping("role")
    @Operation(summary = "获取用户拥有的角色", description = "获取用户拥有的角色")
    @InnerApi
    public Answer<List<Long>> queryUserRole(Long sysUserId) {
        Assert.notNull(sysUserId, "用户id不能为空");
        List<Long> sysRoles = sysUserRoleService.queryUserRole(sysUserId);
        return Answer.success("获取成功", sysRoles);
    }

    @GetMapping("permission")
    @InnerApi
    @Operation(summary = "获取用户拥有的权限编码", description = "获取用户拥有的权限编码")
    public Answer<List<String>> queryUserPermission(Long sysUserId) {
        Assert.notNull(sysUserId, "用户id不能为空");
        List<String> permission = sysUserRoleService.queryUserPermission(sysUserId);
        return Answer.success("获取成功", permission);
    }

    @PostMapping("roleUpdateUser")
    @Operation(summary = "角色修改用户", description = "角色修改用户")
    @OperationLog(title = "角色修改用户", businessType = BusinessTypeEnum.UPDATE)
    public Answer<Void> roleUpdateUser(@RequestBody @Validated(UpdateGroup.class) RoleUserReqVO roleUserReq) {
        sysUserRoleBiz.roleUpdateUser(roleUserReq);
        return Answer.success("保存成功");
    }


    @PostMapping("batchUserAddRoles")
    @Operation(summary = "角色授权", description = "批量给用户增加角色不修改用户原来拥有的角色")
    @OperationLog(title = "批量角色授权", businessType = BusinessTypeEnum.UPDATE)
    public Answer<Void> batchUserAddRoles(@RequestBody @Validated(UpdateGroup.class) UserRoleReqVO userRoleReq) {
        sysUserRoleBiz.batchUserAddRoles(userRoleReq);
        return Answer.success("保存成功");
    }

}

